package drive.Service.ServiceFormat;

import com.google.gson.Gson;
import drive.Mapper.ChatMessageMapper;
import drive.Model.ChatMessage;
import drive.Service.ServiceInterface.ChatMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChatMessageServiceA implements ChatMessageService {
    @Autowired
    private ChatMessageMapper chatMessageMapper;

    private Gson gson = new Gson();

    @Override
    public String findAll() {
        List<ChatMessage> list = chatMessageMapper.findAll();
        return gson.toJson(list);
    }

    @Override
    public String findByMessageID(int messageID) {
        ChatMessage chatMessage = chatMessageMapper.findByMessageID(messageID);
        return gson.toJson(chatMessage);
    }

    @Override
    public String findByChatSessionID(int chatSessionID) {
        List<ChatMessage> list = chatMessageMapper.findByChatSessionID(chatSessionID);
        return gson.toJson(list);
    }

    @Override
    public String findBySenderID(int senderID) {
        List<ChatMessage> list = chatMessageMapper.findBySenderID(senderID);
        return gson.toJson(list);
    }

    @Override
    public String add(ChatMessage chatMessage) {
        chatMessage.setSendTime(new Date());
        int result = chatMessageMapper.add(chatMessage);
        return gson.toJson(result);
    }

    @Override
    public String update(ChatMessage chatMessage) {
        int result = chatMessageMapper.update(chatMessage);
        return gson.toJson(result);
    }

    @Override
    public String delete(int messageID) {
        int result = chatMessageMapper.delete(messageID);
        return gson.toJson(result);
    }
}